<template>
    <view class="container">
        <view id="nearby" class="section section-big nearby">
            <view class="title">附近低功耗蓝牙设备</view>
            <view class="filters"></view>
            <view class="shop" v-for="(item, index) in btDevices" :key="index">
                <navigator :url="'/page/print/print?devId=' + item.devId+'&ydid=' + ydid ">
                    <div class="dev" style="flex-direction: row">
                        <div class="name">
                            {{ item.name }}{{ydid}}
                            <view class="devId">DevId:{{ item.devId }}</view>
                        </div>
                        <div class="rssi" style="vertical-align: middle">
                            <image :src="item.img" />
                            {{ item.rssi }}
                        </div>
                    </div>
                </navigator>
            </view>
        </view>
    </view>
</template>

<script>
var app = getApp();
export default {
    data() {
        return {
			ydid: 0,
            filterId: 1,
            btDevices: app.globalData.btDevices,
            scrollDown: false
        };
    },
    onLoad: function (options) {
		var dhidop = options.ydid;
		console.log('dhid');
		console.log(dhidop);
        var self = this;
        var that = this;
		that.ydid=dhidop;
        for (var i = 0; i < that.btDevices.length; ++i) {
            that.btDevices.pop();
        }
        that.setData({
            btDevices: that.btDevices
        });
        var launchToastDuration;
        if (app.globalData.platform == 'ios') {
            launchToastDuration = 1500;
        } else {
            launchToastDuration = 2500;
        }
        uni.showToast({
            title: 'QR-Printer',
            icon: 'loading',
            mask: true,
            image: '/static/imgs/qr/750x750.png',
            duration: launchToastDuration
        });
    },
    onHide: function () {
        var that = this;
        uni.stopBluetoothDevicesDiscovery({
            success: function (res) {
                console.log(res);
            }
        });
    },
    onShow: function () {
        var that = this;
        if (app.globalData.platform == 'ios') {
            console.log('ios bt');
        } else {
            console.log('android bt');
        }
        uni.openBluetoothAdapter({
            success: function (res) {
                console.log(res);
                uni.startBluetoothDevicesDiscovery({
                    success: function (res) {
                        console.log(res);
                    }
                });
            }
        });
        uni.onBluetoothDeviceFound(function (devices) {
            uni.getBluetoothDevices({
                success: function (res) {
                    var ln = 0;
                    console.log(res, that.btDevices.length);
                    if (that.btDevices.length != null) {
                        ln = that.btDevices.length;
                    }
                    for (var i = ln; i < res.devices.length; ++i) {
                        var rssi_level_img;
                        if (res.devices[i].RSSI > 0 || res.devices[i].name == '未知设备') {
                            continue;
                        }
                        if (res.devices[i].RSSI > -40) {
                            rssi_level_img = '/static/imgs/scan/5.png';
                        } else if (res.devices[i].RSSI > -50) {
                            rssi_level_img = '/static/imgs/scan/4.png';
                        } else if (res.devices[i].RSSI > -60) {
                            rssi_level_img = '/static/imgs/scan/3.png';
                        } else if (res.devices[i].RSSI > -70) {
                            rssi_level_img = '/static/imgs/scan/2.png';
                        } else {
                            rssi_level_img = '/static/imgs/scan/1.png';
                        }
                        var newBtDevice = [
                            {
                                rssi: res.devices[i].RSSI,
                                name: res.devices[i].name,
                                devId: res.devices[i].deviceId,
                                img: rssi_level_img
                            }
                        ];

                        // dump redundant device
                        for (var k = 0; k < that.btDevices.length; ++k) {
                            //console.log('new ',res.devices[i].deviceId,'old',that.data.btDevices[k].devId)
                            if (res.devices[i].deviceId == that.btDevices[k].devId) {
                                //console.log('dump',k,that.data.btDevices[k].devId)
                                that.btDevices.splice(k, 1);
                                break;
                            }
                        }
                        that.btDevices = that.btDevices.concat(newBtDevice);
                    }
                    that.setData({
                        btDevices: that.btDevices
                    });
                    app.globalData.btDevices = that.btDevices;
                }
            });
        });
    },
    onPullDownRefresh: function () {
        // 下拉清空记录，并重新搜索
        console.log('onPullDownRefresh');
        var that = this;
        uni.stopPullDownRefresh();
        uni.stopBluetoothDevicesDiscovery({
            success: function (res) {
                console.log(res);
                uni.closeBluetoothAdapter({
                    success: function (res) {
                        console.log(res);
                        var num = that.btDevices.length;
                        that.btDevices.splice(0, num);
                        that.setData({
                            btDevices: that.btDevices
                        });
                        uni.openBluetoothAdapter({
                            success: function (res) {
                                console.log(res);
                                uni.startBluetoothDevicesDiscovery({
                                    //services: ['FFF0'],
                                    success: function (res) {
                                        console.log(res);
                                    }
                                });
                            }
                        });
                    }
                });
            }
        });
    },
    methods: {
        onScroll: function (e) {
            if (e.detail.scrollTop > 100 && !this.scrollDown) {
                this.setData({
                    scrollDown: true
                });
            } else if (e.detail.scrollTop < 100 && this.scrollDown) {
                this.setData({
                    scrollDown: false
                });
            }
        },

        tapFilter: function (e) {
            switch (e.target.dataset.id) {
                case '2':
                    this.btDevices.sort(function (a, b) {
                        return b.rssi - a.rssi;
                    });
                    break;
            }
            this.setData({
                filterId: e.target.dataset.id,
                btDevices: this.btDevices
            });
        }
    }
};
</script>
<style>
page {
    height: 100%;
}
.container {
    height: 100%;
}

.section-big {
    padding: 20rpx;
}
.section-big .title {
    position: relative;
    text-align: center;
    font-size: 32rpx;
    font-weight: bold;
    line-height: 2;
    margin-bottom: 20rpx;
}

.nearby {
    padding-left: 0;
    padding-right: 0;
}

.nearby .shop {
    padding: 10rpx 0;
    border-top: 1rpx solid #efefef;
}

.nearby .shop .dev {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    padding: 20rpx;
    border-bottom: 2rpx solid #ddd;
    position: relative;
}

.nearby .shop .dev .name {
    padding-left: 20rpx;
}

.nearby .shop .dev .name .devId {
    font-size: 26rpx;
    padding-right: 20rpx;
}

.nearby .shop .dev .rssi {
    font-size: 26rpx;
    padding-right: 20rpx;
}

.nearby .shop .dev .rssi image {
    margin: 0 auto;
    width: 50rpx;
    height: 50rpx;
}
</style>
